package net.alpcontrol.bi.colligation.controller;


import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import cn.afterturn.easypoi.util.PoiPublicUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import net.alpcontrol.bi.colligation.service.IPfProfitService;
import net.alpcontrol.bi.colligation.entity.PfProfit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.alpcontrol.bi.bootstrap.bean.Message;

import java.io.File;
import java.io.FileInputStream;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 丛庆
 * @since 2020-08-31
 */
@CrossOrigin(origins = "*", maxAge = 3600)
@Api(tags = {""})
@RestController
@RequestMapping("/pfProfit")
public class PfProfitController {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private IPfProfitService pfProfitService;

    /**
     * 查询分页数据
     */
    @ApiOperation(value = "查询分页数据")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Message<IPage<PfProfit>> findListByPage(@RequestParam(name = "pageNum", defaultValue = "1") int pageNum,@RequestParam(name = "pageSize", defaultValue = "20") int pageSize){
        Page<PfProfit> page = new Page<>(pageNum, pageSize);
        IPage<PfProfit> result = pfProfitService.selectPage(page);
        return Message.success("查询成功",result);
    }

    /**
     * 根据id查询
     */
    @ApiOperation(value = "根据id查询数据")
    @RequestMapping(value = "/getById", method = RequestMethod.GET)
    public Message<PfProfit> getById(@RequestParam("id") String id){
        return Message.success("查询成功",pfProfitService.getById(id));
    }

    /**
     * 新增
     */
    @ApiOperation(value = "新增数据")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Message add(@RequestBody PfProfit pfProfit){
        pfProfitService.save(pfProfit);
        return Message.success("新增成功");
    }

    /**
     * 删除
     */
    @ApiOperation(value = "删除数据")
    @RequestMapping(value = "/del", method = RequestMethod.DELETE)
    public Message delete(@RequestParam("ids") List<String> ids){
        pfProfitService.removeByIds(ids);
        return Message.success("删除成功");
    }

    /**
     * 修改
     */
    @ApiOperation(value = "更新数据")
    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    public Message update(@RequestBody PfProfit pfProfit){
        pfProfitService.saveOrUpdate(pfProfit);
        return Message.success("修改成功");
    }

    /**
     * 根据年份和公司查询收入、利润、回款、费用、合同、成本的预算值
     * @param year
     * @return
     */
    @ApiOperation(value = "查询预算数据")
    @RequestMapping(value = "/queryBudget", method = RequestMethod.GET)
    public Message queryBudget(@RequestParam("year") String year,@RequestParam("companyId")String companyId){
        return Message.success("0","查询成功",pfProfitService.getBudgetByYear(year,companyId));
    }
    /**
     * 导入预算值
     * @param multipartFile
     * @return
     */
    @ApiOperation(value = "导入预算数据")
    @RequestMapping(value = "/importBudgets", method = RequestMethod.POST)
    public Message importBudgets(@RequestParam("file") MultipartFile multipartFile,
                                 @RequestParam("year") String year,
                                 @RequestParam("companyId") String companyId) throws Exception{

        pfProfitService.importBudgets(multipartFile,year,companyId);
        return Message.success("导入成功");
    }

    /**
     * 根据年份和公司导出预算数据
     * @param year
     * @return
     */
    @ApiOperation(value = "导出预算数据")
    @RequestMapping(value = "/exportBudget", method = RequestMethod.GET)
    public void exportBudget(HttpServletResponse servletResponse,@RequestParam("year") String year, @RequestParam("companyId")String companyId){
        pfProfitService.exportBudget(servletResponse,year,companyId);
    }
}
